!{include file='common/iheader.htm'}
<div id="i_nav">
	<h3 class="page_title">自定义表单 - 编辑</h3>
	<a class="button" href="/manager.php?app=attrs">属性组列表</a>
	<a class="button" href="/manager.php?app=relation">关联属性管理</a>
	<a class="button" href="/manager.php?app=forms">表单管理</a>
	!{if $forms}
	<a class="button" href="/manager.php?app=attrs&act=edit&module=forms">添加自定义表单</a>
	!{/if}
</div>
<form method="post" action="/manager.php?app=forms&act=save" id="form_edit_form" url="/manager.php?app=forms">
	<div class="tabs">
		<ul class="tab_title">
			<li><a href="#base">表单信息</a></li>
			!{if $forms}
			<li><a href="#form_item">自定义属性</a></li>
			<li><a href="#form_group">分组信息</a></li>
			<li><a href="#form_list">列表显示字段</a></li>
			!{/if}
		</ul>
		<div id="base">
			<div class="form_item">
				<strong>表单标题：</strong>
				<div><input class="text_input" maxlength="50" type="text" name="title" value="!{$forms.title}"/></div>
				<p>(名称长度应该在1至50之间)</p>
			</div>
			<div class="form_item">
				<strong>表单名称：</strong>
				<div>
				!{if $forms}
				!{$forms.name}
				<input class="text_input" maxlength="50" type="hidden" name="name" value="!{$forms.name}"/>
				!{else}
				<input class="text_input" maxlength="50" type="text" name="name" value="!{$forms.name}"/>
				!{/if}
				</div>
				<p>(用于代码调用，请使用字母与数字，以字母开头！)</p>
			</div>
			<div class="form_item">
				<strong>表单描述：</strong>
				<div><input class="text_input" maxlength="1000" type="text" name="desc" value="!{$forms.desc}"/></div>
				<p></p>
			</div>
			<div class="form_item">
				<strong>　属性组：</strong>
				<div>
					!{foreach from=$forms.attr_groups item=attr key=attr_id name=c}
					<select name="attrs[]">
						<option value="0">--属性组--</option>
						!{html_options options=$attrs_options selected=$attr_id}
						!{if $smarty.foreach.c.index gt 0}
						<option value="-1">--删除属性组--</option>
						!{/if}
					</select> 
					!{foreachelse}
					<select name="attrs[]">
						<option value="0">--属性组--</option>
						!{html_options options=$attrs_options}
					</select> 
					!{/foreach}
				</div>
				<p></p>
			</div>
			<div class="form_item">
				<strong>多语言：</strong>
				<div><label><input type="checkbox" name="mutil_lan" value="1" !{if $forms.mutil_lan}checked="checked"!{/if}/> 启用</label></div>
				<p>在编辑时同时编辑多种语言，以Tab的方式展示，一般用在后台编辑</p>
			</div>
			<div class="form_item">
				<strong>所有者：</strong>
				<div>
					<select name="owner">
						!{html_options options=$owners selected=$forms.owner}
					</select> 
				</div>
				<p>所有者是系统时，将不允许网站管理员修改、删除</p>
			</div>
			<div class="form_item">
				<strong>表单模型：</strong>
				<div>
					<select name="model">
						!{html_options options=$models selected=$forms.model}
					</select> 
				</div>
				<p>保存时，保存进哪个模型中</p>
			</div>
		</div>
		!{if $forms}
		<div id="form_item">
			<div id="i_nav">
				<a class="button add_attr_btn" href="javascript:void(0)">添加属性</a>
				<a class="button extract_attr_group" href="javascript:void(0)">抽取为属性组</a>
			</div>
			<table class="list_table">
				<tr>
					<th>名称</th>
					<th>类型</th>
					<th>允许为空</th>
					<th>多语言</th>
					<th>操作</th>
				</tr>
				!{foreach from=$forms.items item=item key=code name=attr}
				<tr id="item_!{$code}">
					<td><label><input type="checkbox" name="code[]" value="!{$code}"/> !{$item.name} [!{$code}] !{if $item.desc}<span class="ui-icon ui-icon-info" title="!{$item.desc}"></span>!{/if}</label></td>
					<td>!{$item.type_name}!{if $item.relation_name} - !{$item.relation_name} !{/if} !{if $item.trait}<span>[!{$item.trait_name}]</span>!{/if}</td>
					<td>!{if $item.required eq true}<span class="ui-icon ui-icon-close"></span>!{else}<span class="ui-icon ui-icon-check"></span>!{/if}</td>
					<td>!{if $item.mutil_lan eq true}<span class="ui-icon ui-icon-check"></span>!{else}<span class="ui-icon ui-icon-close"></span>!{/if}</td>
					<td>
						<a href="javascript:void(0)" onclick="attr_item_edit('!{$code}')"><span class="ui-icon ui-icon-pencil"></span>修改</a> 
						<a href="javascript:void(0)" onclick="attr_item_del('!{$forms._id}', '!{$code}')"><span class="ui-icon ui-icon-trash"></span>删除</a>
					</td>
				</tr>
				!{foreachelse}
				<tr><td colspan="5">暂无自定义属性</td></tr>
			!{/foreach}
			</table>
		</div>
		<div id="form_group">
			<div id="i_nav">
				<a class="button add_group_btn" href="javascript:void(0)">添加分组</a>
			</div>
			<table class="list_table" id="tab_item">
				<tr>
					<th>名称</th>
					<th>操作</th>
				</tr>
				!{foreach from=$forms.groups item=item name=attr}
				<tr id="gitem_!{$item._id}" class="item_tr" gid="!{$item._id}">
					<td>
						<a href="javascript:void(0)" onclick="group_edit('!{$item._id}')">!{$item.name}</a> 
						!{if $item.desc}
						<span>(!{$item.desc})</span>
						!{/if}
					</td>
					<td>
						<a href="javascript:void(0)" onclick="group_edit('!{$item._id}')"><span class="ui-icon ui-icon-pencil"></span>修改</a> 
						<a href="javascript:void(0)" onclick="group_del('!{$forms._id}', '!{$item._id}')"><span class="ui-icon ui-icon-trash"></span>删除</a>
					</td>
				</tr>
				!{foreachelse}
				<tr><td colspan="5">暂无自定义属性</td></tr>
			!{/foreach}
			</table>
		</div>
		<div id="form_list">
			<table class="list_table" id="form_list_table">
				<tr>
					<th>名称</th>
					<th>类型</th>
					<th>允许为空</th>
					<th>多语言</th>
				</tr>
				!{foreach from=$list_itmes item=item name=attr}
				<tr id="list_item_!{$item._id}" class="sortable_tr">
					<td><label><input type="checkbox" name="list_view_code[]" value="!{$item._id}" !{if $item.checked}checked="checked"!{/if}/> !{$item.name} [!{$item._id}] !{if $item.desc}<span class="ui-icon ui-icon-info" title="!{$item.desc}"></span>!{/if}</label></td>
					<td>!{$item.type_name}!{if $item.relation_name} - !{$item.relation_name} !{/if} !{if $item.trait}<span>[!{$item.trait_name}]</span>!{/if}</td>
					<td>!{if $item.required eq true}<span class="ui-icon ui-icon-close"></span>!{else}<span class="ui-icon ui-icon-check"></span>!{/if}</td>
					<td>!{if $item.mutil_lan eq true}<span class="ui-icon ui-icon-check"></span>!{else}<span class="ui-icon ui-icon-close"></span>!{/if}</td>
				</tr>
				!{foreachelse}
				<tr><td colspan="5">暂无添加有属性</td></tr>
			!{/foreach}
			</table>
		</div>
		!{/if}
	</div>
	<div class="form_item form_btn_item">
		<input type="hidden" name="id" value="!{$forms._id|default:0}"/>
		<input class="button main" type="button" value="保存" />
	</div>
</form>
!{if $forms}
<div class="dialog" title="自定义表单条目编辑" id="self_attr_item_edit"></div>
!{/if}
!{if $forms.items}
<form method="post" action="/manager.php?app=forms&act=extract_attrs" id="extract_attrs_form" title="抽取属性组" class="dialog">
	<div class="tips ui-state-highlight ui-corner-all">
		抽取属性，会将选中的属性抽取为一组属性。抽取不会影响本表单的内容及展示！
	</div>
	<div class="form_item">
		<strong>组名称：</strong>
		<div><input class="text_input" maxlength="50" type="text" name="name" value=""/></div>
		<p>(名称长度在1至50之间)</p>
	</div>	
	<div class="form_item">
		<strong>组描述：</strong>
		<div><input class="text_input" maxlength="50" type="text" name="desc" value=""/></div>
	</div>
	<div class="form_item form_btn_item">
		<input class="button main" type="button" value="保存" />
		<input type="hidden" name="id" value="!{$forms._id}"/>
		<input type="hidden" name="codes" value="" />
	</div>
</form>
!{/if}
<script type="text/javascript">
	function del(id, code){
		if(confirm('删除后，将可能影响使用，是否继续删除？')){
			var url = '/manager.php?app=forms&act=item_del&id=' + id + '&code=' + code;
			$.get(url, function(data){
				var json = eval('(' + data + ')');
				alert(json.info);
				if(json.result){
					$("#item_" + code).remove();
				}
			});
		}
	}

	function group_del(id, gid){
		if(confirm('删除后，将可能影响使用，是否继续删除？')){
			var url = '/manager.php?app=forms&act=group_del&id=' + id + '&gid=' + gid;
			$.get(url, function(data){
				var json = eval('(' + data + ')');
				alert(json.info);
				if(json.result){
					$("#gitem_" + gid).remove();
				}
			});
		}
	}
	
	function attr_item_edit(code){
		var id = '!{$forms._id|default:0}';
		var url = '/manager.php?app=attrs&act=item_edit&module=forms&id=' + id;
		if(code!=null){
			url += '&code=' + code;
		}
		$("#self_attr_item_edit").html('正在加载，请稍候...').load(url).dialog({width:700,height:540,modal:true,title:'自定义表单条目编辑'});
	}
	
	function attr_item_del(id, code){
		if(confirm('删除后，将可能影响使用，是否继续删除？')){
			var url = '/manager.php?app=attrs&act=item_del&module=forms&id=' + id + '&code=' + code;
			$.get(url, function(data){
				var json = eval('(' + data + ')');
				alert(json.info);
				if(json.result){
					$("#item_" + code).remove();
				}
			});
		}
	}

	$(function(){
		$(".extract_attr_group").click(function(){
			var codes = '';
			$("input[name='code[]']:checked").each(function(){
				codes += $(this).val() + ",";
			});
			if(codes == ''){
				alert('请选择要抽取的属性！');return;
			}
			$("#extract_attrs_form").dialog({width:500,modal:true}).find("input[name='codes']").val(codes);
		});

		//抽取
		$("#extract_attrs_form").ajax_form({
			'submit_btn' : '.main'
		});
		
		$(".add_attr_btn").click(function(){
			attr_item_edit();
		});

		$("select[name='attrs[]']").yu_selector();

		$("#form_edit_form").ajax_form({
			'submit_btn' : '.main',
			'success' : function(form, data){
				window.location.href = '/manager.php?app=attrs&act=edit&module=forms&id=' + data.data;
			}
		});

		$(".add_group_btn").click(function(){
			group_edit();
		});

		$("#tab_item").sortable({
			items : '.item_tr',
			stop: function(event, ui){
				setTimeout(function(){
					var gids = get_group_ids();
					if(cur_gids != gids){
						//有修改，提交到服务器
						var url = '/manager.php?app=forms&act=typesetting&id=!{$forms._id|default:0}';
						$.post(url,{
								codes : gids
							}, function(data){
							var json = eval('(' + data + ')');
							alert(json.info);
							if(json.result){
								
							}
						});
					}
					cur_gids = gids;
				}, 500);
			}
		});

		cur_gids = get_group_ids();
		
		//列表显示项可移动排序
		$("#form_list_table").sortable({
			items : '.sortable_tr'
		});
		
	});

	var cur_gids = '';
	function get_group_ids(){
		var gids = '';
		$('.item_tr').each(function(){
			gids += $(this).attr('gid') + ',';
		});
		return gids;
	}

	function group_edit(gid){
		var id = '!{$forms._id|default:0}';
		var url = '/manager.php?app=forms&act=group_edit&id=' + id;
		if(gid!=null){
			url += '&gid=' + gid;
		}
		$("#self_attr_item_edit").html('正在加载，请稍候...').load(url).dialog({width:640,height:420,modal:true,title:'分组编辑'});
	}
</script>
!{include file='common/ifooter.htm'}